import Vue from 'vue'
import App from './App.vue'
import router from "@/router";
import store from "@/store";

Vue.config.productionTip = false;

// eslint-disable-next-line no-unused-vars
let that;

/**
 * 瞎几把封装的网络请求
 */
import Axios from "axios";
//初始化登录token
Vue.prototype.INIT_LOGIN = function () {
    that = this;
    this.$store.commit("initToken");
    if (this.$store.state.token.trim() === '') {
        this.$router.push({
            path: '/'
        })
    } else {
        console.log(". .. ... ....")
    }
};

function doAxios(e) {
    return Axios.create({
        headers: {
            'user_token': e.getters.token
        }
    });
}

//同步访问
function doaxiosSync(e, url, mode) {
    return Axios({
        url: url,
        method: mode,
        headers: {
            'user_token': e.getters.token
        }
    });
}

Vue.prototype.Axios = Axios;
Vue.prototype.$Axios = doAxios;
Vue.prototype.$AxiosSync = doaxiosSync;

/**
 * sign相关
 */
import Md5 from 'js-md5'

Vue.prototype.doSign = function (json) {
    console.log(".." + store.state.token);
    const j = JSON.stringify(json).replace("/\"/g", "");
    return Md5.hex(j + "123");
};
/**
 * 引入样式
 */
import '@/static/reset.css'

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

//markdown
import hljs from 'highlight.js'
import 'highlight.js/styles/default.css';

Vue.directive('highlight', (el) => {
    let blocks = el.querySelectorAll('pre code');
    blocks.forEach((block) => {
        hljs.highlightBlock(block)
    })
});


/**
 * markdown富文本编辑器
 */
import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'

Vue.use(mavonEditor);

/**
 * api合集
 * @type {string}
 */
// let BASE_API = "http://47.91.165.159:6623";
let BASE_API = "http://127.0.0.1:6623";
//每日一言
let WORDOFTHEDAY_API = "https://api.ly522.com/yan.php?format=json";
// let WORDOFTHEDAY_API = "http://open.iciba.com/dsapi/";
Vue.prototype.WORDOFTHEDAY_API = WORDOFTHEDAY_API;
//必应图片api
Vue.prototype.PHOTOOFTHEDAY_API = "https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=4&mkt=zh-CN";

Vue.prototype.USER_API = BASE_API + "/user";
Vue.prototype.TOOL_API = BASE_API + "/tool";
Vue.prototype.CLASSIFICATION_API = BASE_API + "/classification";
Vue.prototype.ARTICLE_API = BASE_API + "/article";
Vue.prototype.LABEL_API = BASE_API + "/label";
Vue.prototype.COMMENT_API = BASE_API + "/comment";
Vue.prototype.SITE_API = BASE_API + "/site";


router.beforeEach((to, from, next) => {
    /* 路由发生变化修改页面title */
    if (to.meta.title) {
        document.title = to.meta.title
    }
    next()
});


new Vue({
    render: h => h(App),
    store,
    router
}).$mount('#app')